//弹出图片上传窗口

var pathName=window.document.location.pathname;    
//var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);

//跳转页面	
function href(url){
	window.location.href = url;	
	}

function Uploaddialog(returnid, param){
	var path = projectName;
	var par = $.extend({'artid' : 'Uploaddialog'}, param || {});
	var pars = '';
	$.each(par,function(i,v){
		pars += '/'+i+'/'+v;
	});
	
	
    var url = returnUrl+'/uploaddialog/index/returnid/'+returnid+pars;
	var url = returnUrl+'/uploadify/index/returnid/'+returnid+pars;
	art.dialog.open(url, {
		id: par.artid,
		title: '上传文件',
		top:'50%',
		width:'580px',
		height:'400px',
		fixed: true,
		
		lock:true,
		//操作对话框
        button: [{
			name: '确定',
			callback: function () {
				submit_images(artid,returnid);
			},
			focus: true
            },
			
			{
			name: '取消'
			}
			]
		}
	);
	
	art.dialog({title:'模板预览', id:id, iframe:url ,width:'320px',height:'480px'},  function(){art.dialog({id:id}).close()}, function(){art.dialog({id:id}).close()});

}


//图片上传选择确定回调
function submit_images(uploadid,returnid){
	var d = window.top.art.dialog({id:uploadid}).data.iframe;
	var in_content = d.$("#att-status").html().substring(1);
	var in_content = in_content.split('|');
	$('#'+returnid).attr("value",in_content[0]);
}

/*单选框重写*/
function change_radio(obj, val){
	var c = $(obj).parent();
	var input = c.prev('input');
	c.removeClass().addClass('btn-radio btn-radio-'+val);
	input.val(val);
	
}

/*on-off开关*/
function change_switch(obj, url, callback){
	var locked = $(obj).hasClass('locked-1') ? 0 : 1;
	$(obj).removeClass().addClass('locked-'+locked);
	$(obj).next('input[type=text],input[type=hidden]').val(locked);
	$(obj).next('input[type=checkbox],input[type=radio]').prop({'checked': locked ? true : false, 'a': locked});
	
	if (url) {
		$.getJSON(url.replace('-status-', locked), function(data){
			callback && callback(data);
		});
	}
}



/*全选checkbox*/
function checkAll(obj, ids){
	var checked = $(obj).is(':checked') || !$(obj).hasClass('checked');
	$("input[name='"+ids+"']").attr('checked', checked);
	checked ? $(obj).addClass('checked') : $(obj).removeClass('checked')
}
/*反选*/
function checkReverse(obj, ids){
	$("input[name='"+ids+"']").each(function(){
		$(this).attr('checked', !$(this).attr('checked'));
		
	});
}

function art_confirm(str, ok_callback, cannel_callback){
	art.dialog.confirm(str || '你确定要删操作吗？', function () {
		if (ok_callback) ok_callback();
	}, function () {
		if (cannel_callback) cannel_callback();
	});
	
}
/*删除操作确认*/
function confirm_delete(id, obj, tips){
	if ($("input[name='"+id+"']:checked").length==0){
		art.dialog.tips('<font color=red>请勾选后再提交</font>', 2);
		return false;
	}else{
		var _confirm = obj ? $(obj).attr('confirm') : '';
		if (_confirm!='true'){
			if (obj){
				Xconfirm(tips || '该操作是不可恢复的，你确认要操作吗？', function(){
					//alert()
					$(obj).attr('confirm','true').click();
				},
				function(){}
				);
				return false;
			}else{
				return confirm(tips||"删除是不可恢复的，你确认要删除吗？");
			}
		}
	}
}



//毫秒转时间
var format =function   formatData(now)   {     
              var   year=now.getYear();     
              var   month=now.getMonth()+1;     
              var   date=now.getDate();     
              var   hour=now.getHours();     
              var   minute=now.getMinutes();     
              var   second=now.getSeconds();     
              return   year+"-"+month+"-"+date+"   "+hour+":"+minute+":"+second;     
      }








//确认对话框
function Xconfirm(content, yes, no){
	var aa = art.dialog({
		content: content,
		ok: function () {
			yes();
			return false;
		},
		cancelVal: '取消',
		cancel: function(){
			if (no) no();
		}
	});
}

/*管理后台 导航我的位置*/
function admin_location(str){
	$(window.parent.document).contents().find('#admin-nav .location span').html(str);	

}


function UnixTime(t) {
    var time = new Date().getTime();
    if (t) time = Math.round(time / 1000);
    return time
}

function Unix2Time(nS, t, tt) {
    var tm = new Date(parseInt(nS) * t);
    if (tt) tm = tm.toLocaleString().replace(/:d{1,2}$/, ' ');
    return tm;
}

function getDateDiff(dateTimeStamp) {
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var halfamonth = day * 15;
    var month = day * 30;
    var now = new Date().getTime();
    var diffValue = now - dateTimeStamp;
    var monthC = diffValue / month;
    var weekC = diffValue / (7 * day);
    var dayC = diffValue / day;
    var hourC = diffValue / hour;
    var minC = diffValue / minute;
    if (monthC >= 1) {
        result = parseInt(monthC) + "个月前";
    } else if (weekC >= 1) {
        result = parseInt(weekC) + "周前";
    } else if (dayC >= 1) {
        result = parseInt(dayC) + "天前";
    } else if (hourC > 1) {
        result = parseInt(hourC) + "小时前";
    } else result = "刚刚";
    return result;
}
//格式化时间
Date.prototype.format = function(format) {
    var o = {
        "M+": this.getMonth() + 1,
        //month 
        "d+": this.getDate(),
        //day 
        "h+": this.getHours(),
        //hour 
        "m+": this.getMinutes(),
        //minute 
        "s+": this.getSeconds(),
        //second 
        "q+": Math.floor((this.getMonth() + 3) / 3),
        //quarter 
        "S": this.getMilliseconds() //millisecond 
    }

    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}


//桌面提醒
function notify(title, content) {
        
	if(!title && !content){
		title = "桌面提醒";
		content = "您看到此条信息桌面提醒设置成功";
	}
	var iconUrl = "/images/send_ok.png";
	
	if (window.webkitNotifications) {
		//chrome老版本
		if (window.webkitNotifications.checkPermission() == 0) {
			var notif = window.webkitNotifications.createNotification(iconUrl, title, content);
			notif.display = function() {}
			notif.onerror = function() {}
			notif.onclose = function() {}
			notif.onclick = function() {this.cancel();}
			notif.replaceId = 'Meteoric';
			notif.show();
		} else {
			window.webkitNotifications.requestPermission($jy.notify);
		}
	}
	else if("Notification" in window){
		// 判断是否有权限
		if (Notification.permission === "granted") {
			var notification = new Notification(title, {
				"icon": iconUrl,
				"body": content,
			});
		}
		//如果没权限，则请求权限
		else if (Notification.permission !== 'denied') {
			Notification.requestPermission(function(permission) {
				// Whatever the user answers, we make sure we store the
				// information
				if (!('permission' in Notification)) {
					Notification.permission = permission;
				}
				//如果接受请求
				if (permission === "granted") {
					var notification = new Notification(title, {
						"icon": iconUrl,
						"body": content,
					});
				}
			});
		}
	}
}